#include "accounts.h"
#include "ui_accounts.h"
#include <QMessageBox>
#include <QFile>
#include <QTextStream>

Accounts::Accounts(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Accounts)
{
    ui->setupUi(this);
    //connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
    //connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(on_pushButton_2_clicked()));

}

double amount0 = 0;
double standard = 0;
double amount1 = 0;

Accounts::~Accounts()
{
    delete ui;
}

//新建记录；
void Accounts::on_pushButton_clicked()
{
    QString description = ui->descriptionlineEdit->text();
    QString amountStr = ui->amountlineEdit->text();
    bool valid;
    double amount = amountStr.toDouble(&valid);

    if (!description.isEmpty() && valid) {
        QString transaction = description + " - " + amountStr;
        transactions.append(transaction);
        ui->listWidget->addItem(transaction);
        ui->descriptionlineEdit->clear();
        ui->amountlineEdit->clear();

        //这里设计了一个限定额度，但是发现如果删除记录后变化的额度不会返回初始值；
        amount0+=amount;
        if(amount0 >= standard)
        {
            QMessageBox::warning(this, "消费过高", "请注意钱财余额！");
        }
    }
    else {
        QMessageBox::warning(this, "Invalid Input", "Please enter a valid description and amount.");
    }
}


//删除记录；
void Accounts::on_pushButton_2_clicked()
{
    //QString description = ui->descriptionlineEdit->text();
    QString amountStr = ui->amountlineEdit->text();
    bool valid;
    amount1 = amountStr.toDouble(&valid);

    int currentRow = ui->listWidget->currentRow();
    if (currentRow  != -1) {
        //transactions.takeAt(currentRow);
        transactions.removeAt(currentRow);
        ui->listWidget->takeItem(currentRow);
        amount0-=amount1;
    }
}

//获取限定额度；
void Accounts::on_standardEdit_returnPressed()
{
    QString stanDard = ui->standardEdit->text();
    standard = stanDard.toDouble();
}

//当限定额度变化时返回当前值；
void Accounts::on_standardEdit_textChanged(const QString &stanDard)
{
    standard = stanDard.toDouble();
}

// 在 mainwindow.cpp 中添加以下函数
void Accounts::on_loadTrButton_clicked()
{

}


void Accounts::on_saveTrButton_clicked()
{

}

